import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../get/get_extension.dart';
import '../util/toast_util.dart';
import './http.dart';

/// @class : Request
/// @name : hao
/// @description :请求发起
class Request {
  /// 发起GET请求
  /// [url] 请求url
  /// [parameters] 请求参数
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  static void get<T>(
    String url,
    parameters, {
    bool isJson = false,
    bool dialog = true,
    Success<T>? success,
    Fail? fail,
  }) {
    _request(Method.GET, url, parameters,
        isJson: isJson, dialog: dialog, success: success, fail: fail);
  }

  /// 发起POST请求
  /// [url] 请求url
  /// [parameters] 请求参数
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  static void post<T>(
    String url,
    parameters, {
    bool isJson = true,
    bool dialog = true,
    Success<T>? success,
    Fail? fail,
  }) {
    _request(Method.POST, url, parameters,
        isJson: isJson, dialog: dialog, success: success, fail: fail);
  }

  /// 发起请求
  /// [method] 请求类型
  /// [url] 请求url
  /// [parameters] 请求参数
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  static void _request<T>(
    Method method,
    String url,
    parameters, {
    bool isJson = true,
    bool dialog = true,
    Success<T>? success,
    Fail? fail,
  }) {
    if (dialog) {
      Get.showLoading();
    }
    // debugPrint("request url ==============> ${RequestApi.baseurl}$url");

    ///校验参数中是否携带URL
    // parameters.forEach((key, value) {
    //   if (url.contains(key)) {
    //     url = url.replaceAll(':$key', value.toString());
    //   }
    // });
    ///开启请求
    HttpRequest.request(method, url, parameters, isJson: isJson,
        success: (result) {
      if (dialog) {
        Get.dismiss();
      }
      if (success != null) {
        // var resultModel = Result.fromJson(result);
        // debugPrint("request success =>${result}");
        if (result['code'] == 0) {
          success(result['data']);
        } else {
          ///其他状态，弹出错误提示信息
          ToastUtils.show(result['msg']);
        }
      }
    }, fail: (code, msg) {
      debugPrint("request error =>$msg");
      if (dialog) {
        Get.dismiss();
      }
      ToastUtils.show(msg);
      if (fail != null) {
        fail(code, msg);
      }
    });
  }
}

class Request2 {
  /// 发起GET请求
  /// [url] 请求url
  /// [parameters] 请求参数
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  static Future<T> get<T>(
    String url,
    parameters, {
    bool isJson = false,
    bool dialog = true,
    Success<T>? success,
    Fail? fail,
  }) async {
    return await _request2(Method.GET, url, parameters,
        isJson: isJson, dialog: dialog, fail: fail);
  }

  /// 发起POST请求
  /// [url] 请求url
  /// [parameters] 请求参数
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  static Future<T> post<T>(
    String url,
    parameters, {
    bool isJson = true,
    bool dialog = true,
    Success<T>? success,
    Fail? fail,
  }) async {
    return await _request2<T>(Method.POST, url, parameters,
        isJson: isJson, dialog: dialog, fail: fail);
  }

  /// 发起请求
  /// [method] 请求类型
  /// [url] 请求url
  /// [parameters] 请求参数
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  static Future<T> _request2<T>(
    Method method,
    String url,
    parameters, {
    bool isJson = true,
    bool dialog = true,
    Success<T>? success,
    Fail? fail,
  }) async {
    if (dialog) {
      Get.showLoading();
    }
    // debugPrint("request url ==============> ${RequestApi.baseurl}$url");

    ///校验参数中是否携带URL
    // parameters.forEach((key, value) {
    //   if (url.contains(key)) {
    //     url = url.replaceAll(':$key', value.toString());
    //   }
    // });
    ///开启请求
    var result = await HttpRequest.request2(method, url, parameters,
        isJson: isJson, fail: (code, msg) {
      debugPrint("request error =>$msg");
      if (dialog) {
        Get.dismiss();
      }
      ToastUtils.show(msg);
      if (fail != null) {
        fail(code, msg);
      }
    });

    if (dialog) {
      Get.dismiss();
    }

    if (result['code'] == 0) {
      return result['data'] ?? {}; // 如果为空 根据类型返回相应初始值
    } else {
      ///其他状态，弹出错误提示信息
      ToastUtils.show(result['msg']);
      return result['data'];
    }
  }
}

// success: (result) {
    //   if (dialog) {
    //     Get.dismiss();
    //   }
    //   if (success != null) {
    //     // var resultModel = Result.fromJson(result);
    //     // debugPrint("request success =>${result}");
    //     if (result['code'] == 0) {
    //       success(result['data']);
    //     } else {
    //       ///其他状态，弹出错误提示信息
    //       ToastUtils.show(result['msg']);
    //     }
    //   }
    // }